package com.linkhub.oms.admin.modules.logistic.rules;

import cn.hutool.core.collection.CollUtil;
import com.google.common.collect.Lists;
import com.linkhub.oms.admin.modules.logistic.domain.dto.ShippingChannelDTO;
import com.linkhub.oms.admin.modules.logistic.rules.domain.ChannelRuleMatchRequest;
import com.linkhub.oms.admin.modules.logistic.rules.strategy.ChannelRuleMatchStrategy;
import com.linkhub.oms.admin.modules.order.service.OrderInfoAssembleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Description: LogisticChannelRuleManager
 * @Author: echo
 * @Date: 2024/6/1 17:00
 * @Version: 1.0
 */
@Slf4j
@Component
public class LogisticChannelRuleManager {

    @Resource
    private OrderInfoAssembleService orderInfoAssembleService;

    @Resource
    private List<ChannelRuleMatchStrategy> rules;

    public List<ShippingChannelDTO> matchAll(List<ShippingChannelDTO> channels, String sysOrderId) {
        if (CollUtil.isEmpty(channels)) {
            return Lists.newArrayListWithCapacity(0);
        }
        List<ShippingChannelDTO> res = Lists.newArrayList();

        for (ShippingChannelDTO channel : channels) {
            ChannelRuleMatchRequest ruleMatchRequest = ChannelRuleMatchRequest.createDefaultRequest(channel,
                    orderInfoAssembleService.assembleOrder(sysOrderId));
            boolean match = true;
            for (ChannelRuleMatchStrategy rule : rules) {
                match = rule.match(ruleMatchRequest);
                if (!match) {
                    log.info("ChannelRuleMatchStrategy=[{}], match false <<<<<<<<<<<<", rule.getClass().getSimpleName());
                    break;
                }
            }

            if (match) {
                res.add(channel);
            }

        }

        return res;
    }

}
